
;
(function($, $$) {
    $.fn.tabs = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.Tabs);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.Tabs = function(self, options) {
        if (!(this instanceof $$.Tabs)) {
            return new $$.Tabs(self, options);
        }
        var defaults = {

            tabsClass : $$.defaultBorder + " uiCornerAll " + $$.defaultBoxShadow,

            titleBarClass : $$.defaultGradientBG,

            tabsItemWidth : null,

            tabsItemMargin : null,

            closableArray : "all",

            activateIndex : 0,

            tabsItemHoverClass : $$.hoverGradientBG,

            tabsItemSelectedClass : "selected",

            titleList : null,



            change : null,

            close : null,

            leftClick : null,

            rightClick : null,


            tabsItemTotal : 0,

            tabsItemOuterWidth : 0,

            tabsItemViewTotal : 0,

            tabsItemViewIndex : 0,

            titleBarHTML : '<div class="uiTitleBar uiTabsList uiNoSelectText"><div class="uiListContent"><div class="uiTabsItemContainer"></div></div></div>',

            tabsItemHTML : '<div class="uiTabsItem uiCornerTop"></div>',

            contentHTML : '<div class="uiContent"></div>',

            controlHTML : '<div class="uiLeftArrow uiSmallIcon uiArrowLeftIcon"></div><div class="uiRightArrow uiSmallIcon uiArrowRightIcon""></div>',

            closeHTML : '<div class="uiCloseItemBtn uiSmallIcon uiSmallcloseButtonIcon"></div>'
        };
        var opts = $.extend(defaults, options);
        $$.Tabs.prototype.superClass.call(this, self, opts);
        this.init();
    };

    $$.inherit($$.Tabs, $$.Widget, {

        init : function() {
            var tabsObj = this;
            tabsObj.createWidget();
            initTabs(tabsObj.jqObj, tabsObj.opts);
            return tabsObj;
        },

        addItem : function(content, title, index) {
            var tabsObj = this, self = tabsObj.jqObj, opts = tabsObj.opts, contentObj;
            if (content === undefined) {
                return tabsObj;
            }
            contentObj = $(content);
            var itemTarget, contentTarget, insertFunc;
            title = title || contentObj.attr("title");
            if (index === undefined) {
                itemTarget = $("> .uiTabsList > .uiListContent > .uiTabsItemContainer", self);
                contentTarget = self;
                insertFunc = "appendTo";
            }
            else {
                itemTarget = $("> .uiTabsList > .uiListContent > .uiTabsItemContainer > .uiTabsItem", self).eq(index);
                contentTarget = self.children(".uiTabsContent").eq(index);
                insertFunc = "insertBefore";
            }
            $(opts.tabsItemHTML).html(title + opts.closeHTML)[insertFunc](itemTarget);
            $(opts.contentHTML).addClass("uiTabsContent uiHidden").append(contentObj).end()[insertFunc](contentTarget);
            opts.tabsItemTotal++;
            return tabsObj;
        },

        activate : function(index) {
            var tabsObj = this, self = tabsObj.jqObj, opts = tabsObj.opts;
            if (index === undefined) {
                return opts.activateIndex;
            }
            $(">.uiTitleBar >.uiListContent .uiTabsItem:eq(" + index + ")", self).trigger("click.uiTabs");
            return tabsObj;
        },

        item : function(index, content, title) {
            var tabsObj = this, self = tabsObj.jqObj, opts = tabsObj.opts;
            index = index || 0;
            var item = $(">.uiTabsContent", self).eq(index);
            var titleBar = $("> .uiTabsList > .uiListContent > .uiTabsItemContainer >.uiTabsItem", self).eq(index);
            if (content === undefined) {
                return item;
            }
            if (content !== null) {
                item.html(content);
            }
            if (title === undefined) {
                return titleBar;
            }
            titleBar.html(title);
            return tabsObj;
        }
    });

    function initTabs(self, opts) {

        var titleBarObj = $(opts.titleBarHTML).addClass(opts.titleBarClass).append(opts.controlHTML);
        self.addClass("uiTabs uiWidget " + opts.tabsClass).children().each(function(n) {
            var closeHTML = "";
            if (opts.closableArray === "all" || ($.isArray(opts.closableArray) && $.inArray(n, opts.closableArray) !== -1)) {
                closeHTML = opts.closeHTML;
            }
            var title = null;
            var contentObj = $(this).addClass("uiTabsContent uiHidden");
            if ($.isArray(opts.titleList)) {
                title = opts.titleList[n];
            }
            title = title || contentObj.attr("title");
            $(opts.tabsItemHTML).html(title + closeHTML).appendTo(titleBarObj.find("> .uiListContent > .uiTabsItemContainer"));
            opts.tabsItemTotal++;
        });
        self.prepend(titleBarObj);
        var tabsItemList = $("> .uiTabsList > .uiListContent .uiTabsItem", self);
        if (opts.tabsItemWidth !== null) {
            tabsItemList.width(opts.tabsItemWidth);
        }
        if (opts.tabsItemMargin !== null) {
            tabsItemList.css("margin-left", opts.tabsItemMargin);
        }
        opts.tabsItemOuterWidth = tabsItemList.outerWidth(true);
        opts.tabsItemViewTotal = Math.floor($("> .uiTabsList", self).width() / opts.tabsItemOuterWidth);
        if (opts.tabsItemTotal > opts.tabsItemViewTotal) {
            $("> .uiTitleBar > .uiRightArrow", self).show();
        }
        if (self.height() > titleBarObj.height()) {
            var contentObj = self.children(".uiTabsContent");
            contentObj.height(self.height() - titleBarObj.height() - parseInt(contentObj.css("margin-top")) - parseInt(contentObj.css("margin-bottom")));
        }


        initEvent(self, opts);
        var disableValue = opts.disabled;
        opts.disabled = false;
        $("> .uiTabsList > .uiListContent > .uiTabsItemContainer >.uiTabsItem", self).eq(opts.activateIndex).click();
        opts.disabled = disableValue;
        if (opts.disabled) {
        }
        return self;
    }


    function initEvent(self, opts) {
        $("> .uiTabsList > .uiListContent > .uiTabsItemContainer", self).bind("click.uiTabs", function(e) {
            if (opts.disabled) {
                return false;
            }
            var target = $(e.target), index;
            if (target.hasClass("uiCloseItemBtn")) {

                var obj = target.parent(".uiTabsItem");
                if ($.isFunction(opts.close)) {
                    if (opts.close.call(self[0], obj, e) === false) {
                        return false;
                    }
                }
                if (obj.next().length !== 0) {
                    obj.next().click();
                }
                else {
                    obj.prev().click();
                }
                index = obj.index();
                self.children(".uiTabsContent:eq(" + index + ")").remove();
                obj.remove();
                opts.tabsItemTotal--;
                checkItemViewStatus(self, opts);
                return false;
            }
            else
            if (target.hasClass("uiTabsItem")) {
                index = target.addClass(opts.tabsItemSelectedClass).siblings(".selected").removeClass(opts.tabsItemSelectedClass).end().index();
                var content = self.children(".uiTabsContent:eq(" + index + ")");
                if ($.isFunction(opts.change)) {
                    if (opts.change.call(self[0], target, content, e) === false) {
                        return false;
                    }
                }
                content.removeClass("uiHidden").siblings(".uiTabsContent").addClass("uiHidden");
                opts.activateIndex = index;
                return;
            }
        });
        $("> .uiTitleBar", self).bind("click.uiTabs", function(e) {
            if (opts.disabled) {
                return false;
            }
            var target = $(e.target), clickFunc = "";
            if (target.hasClass("uiRightArrow")) {
                clickFunc = "rightClick";
                opts.tabsItemViewIndex++;
            }
            else
            if (target.hasClass("uiLeftArrow")) {
                clickFunc = "leftClick";
                opts.tabsItemViewIndex--;
            }
            if (opts.tabsItemViewIndex < 0) {
                opts.tabsItemViewIndex = 0;
                return;
            }
            else
            if (opts.tabsItemTotal - opts.tabsItemViewTotal < opts.tabsItemViewIndex) {
                opts.tabsItemViewIndex = opts.tabsItemTotal - opts.tabsItemViewTotal;
                return;
            }
            if (clickFunc !== "") {
                if ($.isFunction(opts[clickFunc])) {
                    if (opts[clickFunc].call(self[0], target, e) === false) {
                        return false;
                    }
                }
                var scrollLeftValue = opts.tabsItemOuterWidth * opts.tabsItemViewIndex;
                target.siblings(".uiListContent").stop().animate({left : -scrollLeftValue}, opts.animateTime);
                checkItemViewStatus(self, opts);
            }
        });
        $("> .uiTabsList", self).bind("mousewheel.uiTabs", function(e, delta) {
            if (opts.disabled) {
                return false;
            }
            if (delta > 0) {
                $("> .uiLeftArrow", this).click();
            }
            else {
                $("> .uiRightArrow", this).click();
            }
            return false;
        });
        $("> .uiTabsList > .uiListContent > .uiTabsItemContainer > .uiTabsItem", self).hover(function() {
            if (opts.disabled) {
                return false;
            }
            $(this).addClass(opts.tabsItemHoverClass);
        }, function() {
            if (opts.disabled) {
                return false;
            }
            $(this).removeClass(opts.tabsItemHoverClass);
        });
        return self;
    }


    function checkItemViewStatus(self, opts) {

        if (opts.tabsItemTotal - opts.tabsItemViewTotal <= opts.tabsItemViewIndex) {
            if ($.browser.opera)
            {
                $("> .uiTitleBar > .uiRightArrow", self).css("opacity", 0);
            }
            else {
                $("> .uiTitleBar > .uiRightArrow", self).hide();
            }
        }
        else {
            if ($.browser.opera) {
                $("> .uiTitleBar > .uiRightArrow", self).css("opacity", 1);
            }
            else {
                $("> .uiTitleBar > .uiRightArrow", self).show();
            }
        }
        if (opts.tabsItemViewIndex <= 0) {
            $("> .uiTitleBar > .uiLeftArrow", self).hide();
        }
        else {
            $("> .uiTitleBar > .uiLeftArrow", self).show();
        }
        return self;
    }
})(jQuery, BASE);